SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE dbo.[DnnProduct_CDS_AssetsStampConfigUpdate]
	@DistributorID varchar(50),
	@AssetID int
AS
BEGIN
	DECLARE @PermID varchar(50)
	DECLARE @TemplateID int
	DECLARE @DRMType tinyint
	
	SELECT @PermID = PermID, @TemplateID = A.TemplateID, @DRMType = A.DRMType FROM [DISC_DistProducts] P
	JOIN [DnnProduct_CDS_Assets] A ON P.DistID = A.DistributorID AND A.ID = @AssetID
	WHERE P.DistID = @DistributorID AND P.PermID = A.Identifier

	IF (LEN(@PermID) > 0)
	BEGIN
	IF (@DRMType = 3) -- Stamp
	BEGIN
		DELETE FROM [DISC_StampConfig] WHERE [distID] = @DistributorID AND [permID] = @PermID

		INSERT INTO [DISC_StampConfig] ([distID], [permID], [staticString], [fontName], [fontSize], [textRenderingMode],
			   [textColor], [textStrokeColor], [strokeWidth], [placeAs], [x], [y], [units], [rotate], [opacity],
			   [fontEmbed], [fontSubset], [stampType], [textColorSpace], [strokeColorSpace], [pages])
		SELECT [distID], @PermID, [staticString], [fontName], [fontSize], [textRenderingMode],
			[textColor], [textStrokeColor], [strokeWidth], [placeAs], [x], [y], [units], [rotate], [opacity],
			[fontEmbed], [fontSubset], [stampType], [textColorSpace], [strokeColorSpace], [pages]
		FROM [DISC_DefaultStampConfig]
		WHERE [ID] IN (SELECT [DefaultStampConfigID] FROM [DnnProduct_CDS_StampSets] WHERE [TemplateID] = @TemplateID)
		UNION ALL
		SELECT @DistributorID, @PermID, '', '', 0, 0, '', '', 0, 0, '', '', 0, 0, 0, 0, 0, 0, 0, 0, ''
		FROM [DnnProduct_CDS_StampSets] WHERE [TemplateID] = @TemplateID AND [DefaultStampConfigID] IS NULL
	END
	ELSE IF (@DRMType = 4) -- Epub
	BEGIN
		DELETE FROM [DISC_DRMStampConfig] WHERE [distID] = @DistributorID AND [permID] = @PermID

		INSERT INTO [DISC_DRMStampConfig] ([distID], [permID], [DRMType], [config])
		SELECT [distID], @PermID, [DRMType], [config]
		FROM [DISC_DefaultDRMStampConfig]
		WHERE [ID] IN (SELECT [DefaultDRMStampConfigID] FROM [DnnProduct_CDS_StampSets] WHERE [TemplateID] = @TemplateID)
		UNION ALL
		SELECT @DistributorID, @PermID, (SELECT DRMType FROM [DISC_DistProducts] P WHERE P.DistID = @DistributorID AND P.PermID = @PermID), ''
		FROM [DnnProduct_CDS_StampSets]
		WHERE [TemplateID] = @TemplateID AND [DefaultDRMStampConfigID] IS NULL
	END
	END
END
GO
